﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace XDUD_QuanLyBanHang
{
    public partial class frmQuanlydanhmuckhachhangtheonhomthanhpho : Form
    {
        string strConnectionString =
            "Data Source=VUHOANG-PC\\SQLEXPRESS;Initial Catalog=QuanLyBanHang;Integrated Security=True";
        SqlConnection conn = null;
        SqlDataAdapter daThanhPho = null;
        DataTable dtThanhPho = null;
        SqlDataAdapter daKhachHang = null;
        DataTable dtKhachHang = null;
        DataView dtv = null;

        public frmQuanlydanhmuckhachhangtheonhomthanhpho()
        {
            InitializeComponent();
        }
        void LoadData()
        {
            //dtv.RowFilter = "";
            try
            {
                conn = new SqlConnection(strConnectionString);

                daThanhPho = new SqlDataAdapter("Select * From ThanhPho", conn);
                dtThanhPho = new DataTable();
                dtThanhPho.Clear();
                daThanhPho.Fill(dtThanhPho);
                (dgvKhachHang.Columns["ThanhPho"] as DataGridViewComboBoxColumn).DataSource = dtThanhPho;

                (dgvKhachHang.Columns["ThanhPho"] as
                    DataGridViewComboBoxColumn).DisplayMember = "TenThanhPho";
                (dgvKhachHang.Columns["ThanhPho"] as
                    DataGridViewComboBoxColumn).ValueMember = "ThanhPho";
                cboThanhPho.DataSource = dtThanhPho;
                cboThanhPho.DisplayMember = "TenThanhPho";
                cboThanhPho.ValueMember = "ThanhPho";


                daKhachHang = new SqlDataAdapter("Select KhachHang.MaKh,TenCty,DiaChi,DienThoai, ThanhPho.ThanhPho FROM KhachHang, ThanhPho where KhachHang.ThanhPho = ThanhPho.ThanhPho", conn);
                dtKhachHang = new DataTable();
                dtKhachHang.Clear();
                daKhachHang.Fill(dtKhachHang);
                dtv = new DataView(dtKhachHang);

                dgvKhachHang.DataSource = dtv;

                //dtv.RowFilter = "";
                //conn = new SqlConnection(strConnectionString);
                //daKhachHang = new SqlDataAdapter("Select * FROM KhachHang", conn);
                //dtKhachHang = new DataTable();
                //dtKhachHang.Clear();
                //daKhachHang.Fill(dtKhachHang);
                //dtv = new DataView(dtKhachHang);
                //(dgvKhachHang.Columns["ThanhPho"] as DataGridViewComboBoxColumn).DataSource = dtv;

                //(dgvKhachHang.Columns["ThanhPho"] as
                //    DataGridViewComboBoxColumn).DisplayMember = "ThanhPho";
                //(dgvKhachHang.Columns["ThanhPho"] as
                //    DataGridViewComboBoxColumn).ValueMember = "ThanhPho";
                //cboThanhPho.DataSource = dtv;
                //cboThanhPho.DisplayMember = "ThanhPho";
                //cboThanhPho.ValueMember = "ThanhPho";
                //dgvKhachHang.DataSource = dtv;

            }
            catch (SqlException)
            {

            }
        }

        private void frmQuanlydanhmuckhachhangtheonhomthanhpho_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            dtv.RowFilter = "ThanhPho ='" + cboThanhPho.SelectedValue.ToString() + "'";
            dgvKhachHang.DataSource = dtv;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
